// GeoLocation 정보를 가져오기위한 Javascript
// 작성자 : 이종현 (dlab82@pulipinc.com)
// 작성일 : 2011-03-24

	var watchId;
	var sendFlag = false;
	var rCountry = "";
	var rLocality = "";
	var rSubLocality = "";
	var rSubLocality2 = "";
	
	var lat = "";
	var lng = "";
	
	var handleFunc = function (rCountry, rLocality, rSubLocality, rSubLocality2) {
		
	};
	
	var positionHandler = function (lat, lng) {
		
	};

	function startTracking() {
		if (!!navigator.geolocation) {
			// 현재 위치 요청
			// 참조 : http://dev.w3.org/geo/api/spec-source.html
			watchId = navigator.geolocation.getCurrentPosition(getLocationInfo, errorCallback, {enableHighAccuracy:true, maximumAge:0, timeout:10000});
		} else {
			if (!sendFlag) {
				sendFlag = true;
				resultRender(rCountry, rLocality, rSubLocality, rSubLocality2);
			}
		}
	}
	
	function getPosition() {
		if (!!navigator.geolocation) {
			// 현재 위치 요청
			// 참조 : http://dev.w3.org/geo/api/spec-source.html
			watchId = navigator.geolocation.getCurrentPosition(setLocation, function() {positionHandler(lat, lng);} , {enableHighAccuracy:true, maximumAge:0, timeout:10000});
		} else {
			positionHandler(lat, lng);
		}
	}
	
	function errorCallback(error)
	{
		resultRender(rCountry, rLocality, rSubLocality, rSubLocality2);
	}
	
	function setLocation(position) {
		var latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
		var lat = position.coords.latitude;
		var lng = position.coords.longitude;
		
		positionHandler(lat, lng);
	}
	
	function getLocationInfo(position) {
		var latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
		var lat = position.coords.latitude;
		var lng = position.coords.longitude;
		
		var geocoder;
		geocoder = new google.maps.Geocoder();

		if (geocoder) {
			geocoder.geocode({'latLng': latLng}, function(results, status) {
				
				if (status == google.maps.GeocoderStatus.OK) {
					if (results[0]) {
						for(var i = 0; i < results[0].address_components.length; i++) {
							if (results[0].address_components[i].types[0] == "country") {
								rCountry = results[0].address_components[i].long_name;
							} else if (results[0].address_components[i].types[0] == "locality") {
								rLocality = results[0].address_components[i].long_name;
							} else if (results[0].address_components[i].types[0] == "sublocality") {
								if (rSubLocality2 == "") rSubLocality2 = results[0].address_components[i].long_name;
								else rSubLocality = results[0].address_components[i].long_name;
							}
						}
					}
				}

				if (!sendFlag) {
					sendFlag = true;
					resultRender(rCountry, rLocality, rSubLocality, rSubLocality2);
				}
			});
		}
	}
	
	function resultRender(rCountry, rLocality, rSubLocality, rSubLocality2) {
		handleFunc(rCountry, rLocality, rSubLocality, rSubLocality2);
	}